package com.whut.whw.post;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.bean.City;
import com.whut.whw.bean.Province;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by 张芳容 on 2017/06/12.
 */

public class AreaSelectActivity extends BaseActivity {
//    //返回的数据
//    String r_province;
//    String r_city;
//    String r_city_ZipCode;
    //省市数据
    private ArrayList<Province> provinces;
    private ArrayList<ArrayList<City>> cities;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.refresh_list_view)
    ExpandableListView refresh_list_view;
    @BindView(R.id.loadingBar)
    ProgressBar loadingBar;
    @Override
    public int getLayoutId() {
        return R.layout.activity_area_select;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initToolbar();
        //获取国省份数据
        provinces=ReadXML.ReadProvinces();
        cities=ReadXML.ReadCities(provinces);
//        LogUtils.e("省市",String.valueOf(provinces.size()));
        loadingBar.setVisibility(View.INVISIBLE);
        //示例
        final ExpandableListAdapter adapter = new BaseExpandableListAdapter()
        {
            // 获取指定组位置、指定子列表项处的子列表项数据
            @Override
            public Object getChild(int groupPosition, int childPosition)
            {
               // return arms[groupPosition][childPosition];
                return cities.get(groupPosition).get(childPosition);
            }

            @Override
            public long getChildId(int groupPosition, int childPosition)
            {
                return childPosition;
            }

            @Override
            public int getChildrenCount(int groupPosition)
            {
                //return arms[groupPosition].length;
                return cities.get(groupPosition).size();
            }

            private TextView getGroupTextView()
            {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 130);
                TextView textView = new TextView(AreaSelectActivity.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                textView.setPadding(40, 0, 0, 0);
                textView.setTextSize(16);
                textView.setTextColor(ContextCompat.getColor(context,R.color.text_444));
                return textView;
            }
            private TextView getChildTextView()
            {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 110);
                TextView textView = new TextView(AreaSelectActivity.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                textView.setPadding(60, 0, 0, 0);
                textView.setTextSize(14);
                textView.setTextColor(ContextCompat.getColor(context,R.color.text_666));
                return textView;
            }

            private ImageView getDivider(){
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 1);
                ImageView imageView=new ImageView(AreaSelectActivity.this);
                imageView.setBackgroundColor(ContextCompat.getColor(context,R.color.text_999));
                return imageView;
            }
            // 该方法决定每个子选项的外观
            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent)
            {
                TextView textView = getChildTextView();
                textView.setText(((City)getChild(groupPosition, childPosition)).getCityName());
                textView.setTag(((City)getChild(groupPosition, childPosition)).getZipCode());
                return textView;
            }

            // 获取指定组位置处的组数据
            @Override
            public Object getGroup(int groupPosition)
            {
               // return armTypes[groupPosition];
                return provinces.get(groupPosition);
            }

            @Override
            public int getGroupCount()
            {
                //return armTypes.length;
                return provinces.size();
            }

            @Override
            public long getGroupId(int groupPosition)
            {
                return groupPosition;
            }

            // 该方法决定每个组选项的外观
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent)
            {
                DisplayMetrics dm =getResources().getDisplayMetrics();
                int width = dm.widthPixels;
               // ToastUtils.showShort("长度是"+width);
                refresh_list_view.setIndicatorBounds(width-40, width);
                LinearLayout ll = new LinearLayout(AreaSelectActivity.this);
//                ll.setOrientation(LinearLayout.VERTICAL);
//                ll.setOrientation(0);
                TextView textView = getGroupTextView();
//                textView.setText(getGroup(groupPosition).toString());
                textView.setText(((Province)getGroup(groupPosition)).getProvinceName());
                textView.setTag(((Province)getGroup(groupPosition)).getID());
                ll.addView(textView);
                return ll;
            }

            @Override
            public boolean isChildSelectable(int groupPosition,
                                             int childPosition)
            {
                return true;
            }

            @Override
            public boolean hasStableIds()
            {
                return true;
            }
        };
        refresh_list_view.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < adapter.getGroupCount(); i++) {
                    if (groupPosition != i) {
                        refresh_list_view.collapseGroup(i);
                    }
                }
            }
        });
        refresh_list_view.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            /**  * 参数1：控件的父对象  * 参数2：子的行布局对象  * 参数3：子节点所在的组的索引 * 参数4：子节点的索引
             */
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //ToastUtils.showShort("你选中了");
                Intent intent=getIntent();
                Bundle bundle=new Bundle();
                bundle.putString("province",((Province)(adapter.getGroup(groupPosition))).getProvinceName());
                bundle.putString("city",((City)(adapter.getChild(groupPosition,childPosition))).getCityName());
                bundle.putString("zipCode",((City)(adapter.getChild(groupPosition,childPosition))).getZipCode());
                //再把bundle中的数据传给intent，以传输过去
                intent.putExtras(bundle);
                setResult(RESULT_OK,intent);
                finish();
                return true;
            }
        });
        refresh_list_view.setAdapter(adapter);
    }

    /**
     * 初始化toolbar
     */
    private void initToolbar(){
        toolbar.setNavigationIcon(R.drawable.rc_back_icon);
        toolbar.setTitle(R.string.good_area_hint);
        toolbar.setTitleTextColor(ContextCompat.getColor(context,R.color.white));
    }

    @Override
    public void setListener() {

        //点击返回
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaSelectActivity.this.finish();
            }
        });

    }
}
